package com.wxh.springmvc.allrequestpattern.controller;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName: SpringMvcAllRequestPatternController
 * @Description: springmvc 所有请求方式
 * @Author wxh
 * @Date: 2020/8/6 10:32
 * @Version V1.0.0
 * @Since 1.8
 */
@Controller
@RequestMapping(value = "/springmvc")
public class SpringMvcAllRequestPatternController {

    private final Logger LOGGER = LoggerFactory.getLogger(SpringMvcAllRequestPatternController.class);

    /**
     * 接收json对象，spring使用@RequestParam注解接收，可以不写默认会加上
     * @param user json对象
     * @return String
     */
    @RequestMapping(value = "/jsonObject", method = RequestMethod.POST)
    @ResponseBody
    public String jsonObject(@RequestParam(value = "user") String user){
        /**
         *  前端传参：{user: "{"name":"zs","age":"18"}"}
         * request.getParameter("name") 等价于 @RequestParam 等价于ModelAttribute(针对很多参数时候)
         */
        LOGGER.debug("前端传入参数：{}",user);
        Map<String, Object> userinfo = JSON.parseObject(user, Map.class);
        String name = userinfo.get("name").toString();
        return JSON.toJSONString(name);
    }

    /**
     * 接收json字符串，spring使用@RequestParam注解接收，可以不写默认会加上
     * @param params json字符串对象
     * @return String
     */
    @RequestMapping(value = "/jsonString", method = RequestMethod.POST)
    @ResponseBody
    public String jsonString(@RequestBody Map<String,Object> params){
        /**
         * 前端传参：{name: "zs", age: "18"}
         * HttpInputMessage----> HttpMessageConverter----> HttpOutputMessage
         */
        LOGGER.debug("前端传入参数：{}",params);
        String name = params.get("name").toString();
        return JSON.toJSONString(name);
    }

}
